
package br.unipampa.view;

import br.unipampa.model.Estudante;
import br.unipampa.utilitarios.TipoDeUsuario;
import br.unipampa.model.Universidade;
import br.unipampa.utilitarios.ExecutaSom;
import br.unipampa.utilitarios.ImplementaIcone;
import br.unipampa.utilitarios.LimparCampos;
import br.unipampa.utilitarios.VerificarCampos;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

/**
 * JFrame responsável por cadastrar usuários, através dele o usuário administrador
 * insere as informações básicas de um novo usuário a ser cadastrado.
 * 
 * @author Gabriel Moro
 * @since 15/03/2013
 * @version 1.2
 */
public class CadastroDeUsuarios extends javax.swing.JFrame implements LimparCampos,VerificarCampos,ImplementaIcone,EfeitosSonoros {

    private Estudante estudante;
    private Universidade universidade;
    private Image iconeTitulo;

    /**
     * O construtor inicializa os devidos componentes.
     */
    public CadastroDeUsuarios() {

        initComponents();

           
         
        addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                ApoioDeInterface.mensagemSair();
            }
        });
            
        tratarIcone();
        
        setResizable(false);
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnVoltar = new javax.swing.JButton();
        jPanelInterno = new javax.swing.JPanel();
        lblNome = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jftxtMatricula = new javax.swing.JFormattedTextField();
        lblNomeUniversidade = new javax.swing.JLabel();
        txtNomeUniversidade = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtEndUniver = new javax.swing.JTextField();
        txtLogin = new javax.swing.JTextField();
        lblLogin = new javax.swing.JLabel();
        lblConfirmSenha = new javax.swing.JLabel();
        btnLimpar = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        lblSenha = new javax.swing.JLabel();
        jpassConf = new javax.swing.JPasswordField();
        jpassSenha = new javax.swing.JPasswordField();
        jPanel1 = new javax.swing.JPanel();
        lblTipoUsuario = new javax.swing.JLabel();
        jCombroTipoUsuario = new javax.swing.JComboBox();
        lblImage = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Cadastro de Usuário");

        btnVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/voltar.png"))); // NOI18N
        btnVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarActionPerformed(evt);
            }
        });

        jPanelInterno.setBackground(new java.awt.Color(204, 204, 255));

        lblNome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblNome.setText("Nome:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Matrícula:");

        lblNomeUniversidade.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblNomeUniversidade.setText("Nome da Universidade:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("Endereço da Universidade:");

        lblLogin.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblLogin.setText("Login:");

        lblConfirmSenha.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblConfirmSenha.setText("Confirme a Senha:");

        btnLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/eraserImg.png"))); // NOI18N
        btnLimpar.setToolTipText("Limpar");
        btnLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparActionPerformed(evt);
            }
        });

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/saveCadastro.png"))); // NOI18N
        btnSalvar.setToolTipText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        lblSenha.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblSenha.setText("Senha:");

        lblTipoUsuario.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblTipoUsuario.setText("Tipo de Usuário:");

        jCombroTipoUsuario.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-------------------", "Administrador", "Usuário Comum" }));
        jCombroTipoUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCombroTipoUsuarioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTipoUsuario)
                .addGap(18, 18, 18)
                .addComponent(jCombroTipoUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(134, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTipoUsuario)
                    .addComponent(jCombroTipoUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanelInternoLayout = new javax.swing.GroupLayout(jPanelInterno);
        jPanelInterno.setLayout(jPanelInternoLayout);
        jPanelInternoLayout.setHorizontalGroup(
            jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelInternoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelInternoLayout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelInternoLayout.createSequentialGroup()
                                .addComponent(lblSenha)
                                .addGap(18, 18, 18)
                                .addComponent(jpassSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanelInternoLayout.createSequentialGroup()
                                .addComponent(lblConfirmSenha)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jpassConf, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addComponent(lblImage, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE))
                    .addGroup(jPanelInternoLayout.createSequentialGroup()
                        .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelInternoLayout.createSequentialGroup()
                                .addComponent(lblNome)
                                .addGap(18, 18, 18)
                                .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanelInternoLayout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jftxtMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanelInternoLayout.createSequentialGroup()
                                .addComponent(lblNomeUniversidade)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtNomeUniversidade, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanelInternoLayout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtEndUniver, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanelInternoLayout.createSequentialGroup()
                                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanelInternoLayout.createSequentialGroup()
                                .addComponent(lblLogin)
                                .addGap(18, 18, 18)
                                .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(jPanelInternoLayout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanelInternoLayout.setVerticalGroup(
            jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelInternoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNome)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jftxtMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNomeUniversidade)
                    .addComponent(txtNomeUniversidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtEndUniver, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblLogin)
                    .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanelInternoLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblSenha)
                            .addComponent(jpassSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblConfirmSenha)
                            .addComponent(jpassConf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelInternoLayout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(lblImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jPanelInterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanelInterno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(4, 4, 4))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarActionPerformed
        efeitoSonoro();
        CadastroDeUsuarios.this.dispose();
    }//GEN-LAST:event_btnVoltarActionPerformed

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparActionPerformed
        efeitoSonoro();
        limparCampos();
    }//GEN-LAST:event_btnLimparActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        efeitoSonoro();
        if (verificaCampos()) {
            try {
                String login = txtLogin.getText();
                String nome = txtNome.getText();
                String matricula = jftxtMatricula.getText();
                this.universidade = new Universidade(txtNomeUniversidade.getText().toString().toUpperCase(),
                        txtEndUniver.getText().toString().toLowerCase());

                TipoDeUsuario tipo = validationOfTypeUser();
                if (validationOfPassword()) {
                    char[] senha = jpassSenha.getPassword();
                    String senha1Text = String.valueOf(senha);

                    this.estudante = new Estudante(login, senha1Text, nome, this.universidade, matricula, tipo);
                }
                if (this.estudante.salvarDados()) {
                    ApoioDeInterface.mensagemDeSucesso();
                    limparCampos();
                } else {
                    throw new SQLException("Erro de inserção de dados!");
                }
            } catch (SQLException ex) {
                ApoioDeInterface.mensagemErroBanco(ex.getMessage());
            }
        } else {
            ApoioDeInterface.mensagemIncopatibilidadeSenha();
            limparCampos();
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void jCombroTipoUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCombroTipoUsuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCombroTipoUsuarioActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLimpar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JButton btnVoltar;
    private javax.swing.JComboBox jCombroTipoUsuario;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelInterno;
    private javax.swing.JFormattedTextField jftxtMatricula;
    private javax.swing.JPasswordField jpassConf;
    private javax.swing.JPasswordField jpassSenha;
    private javax.swing.JLabel lblConfirmSenha;
    private javax.swing.JLabel lblImage;
    private javax.swing.JLabel lblLogin;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblNomeUniversidade;
    private javax.swing.JLabel lblSenha;
    private javax.swing.JLabel lblTipoUsuario;
    private javax.swing.JTextField txtEndUniver;
    private javax.swing.JTextField txtLogin;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtNomeUniversidade;
    // End of variables declaration//GEN-END:variables

    @Override
    public void limparCampos() {
        txtNome.setText("");
        txtNomeUniversidade.setText("");
        txtLogin.setText("");
        txtEndUniver.setText("");
        jpassSenha.setText("");
        jpassConf.setText("");
        jftxtMatricula.setText("");
        jCombroTipoUsuario.setSelectedIndex(0);
    }

    private TipoDeUsuario validationOfTypeUser() {
        int resposta = this.jCombroTipoUsuario.getSelectedIndex();
        if (resposta == 1) {
            return TipoDeUsuario.ADMINISTRADOR;
        } else {
            return TipoDeUsuario.COMUM;
        }
    }

    private boolean validationOfPassword() {
        char[] senha = jpassSenha.getPassword();
        String senhaTxt = String.valueOf(senha);

        char[] senha2 = jpassConf.getPassword();
        String senhaTxt2 = String.valueOf(senha2);

        if (senhaTxt.equalsIgnoreCase(senhaTxt2)) {
            return true;
        } else {
            return false;
        }
    }

    public boolean verificaCampos() {
        if (txtNome.getText() == null
                || txtLogin.getText() == null
                || txtNomeUniversidade.getText() == null
                || this.jCombroTipoUsuario.getSelectedIndex() == 0
                || jpassSenha.getPassword() == null
                || jpassConf.getPassword() == null) {
            lblNome.setText("*Nome:");
            lblLogin.setText("*Login:");
            lblNomeUniversidade.setText("*Nome da Universidade:");
            lblTipoUsuario.setText("*Tipo de Usuário:");
            lblSenha.setText("*Senha:");
            lblConfirmSenha.setText("*Confirme a Senha:");
            return false;
        } else {
            return true;
        }
    }
     @Override
    public void tratarIcone() {
        //definindo icone do sistema
        this.iconeTitulo= Toolkit.getDefaultToolkit().getImage(
                getClass().getResource("/br/unipampa/icons/iconeOfSystem.png"));
        
        this.setIconImage(this.iconeTitulo);
    }

    @Override
    public void efeitoSonoro() {
        ExecutaSom exeSound= new ExecutaSom();
        exeSound.executar("/br/unipampa/sounds/click1.wav");
    }
}